package com.qianxing.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qianxing.dto.MessageReviewDTO;
import com.qianxing.exceptionhandler.ResponseException;
import com.qianxing.service.TbMsgReviewService;
import com.qianxing.service.TbUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.qianxing.utils.RedisContanct.LOGIN_USER_KEY;

/**
 * @Author:JasonLong
 * @Date:2023/7/10
 * @Time:17:27
 */
@RestController
@RequestMapping("/msgReview")
public class WxMsgReviewController {

    private Logger LOGGER = LoggerFactory.getLogger(WxMsgReviewController.class);

    @Autowired
    private TbMsgReviewService msgReviewService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 保存用户评论
     *
     * @param reviewDTO
     * @return
     */
    @RequestMapping("/saveReview")
    public ResponseException saveReview(@RequestBody MessageReviewDTO reviewDTO) {
        LOGGER.info("===Get Data:===" + reviewDTO);
        //从缓存获取user的OpenId
        String userOpenId = stringRedisTemplate.opsForValue().get(LOGIN_USER_KEY + reviewDTO.getToken());
        reviewDTO.setUserOpenId(userOpenId);
        return msgReviewService.saveReview(reviewDTO);
    }

    /**
     * 获取用户评论接口
     *
     * @param msgId
     * @return
     */
    @RequestMapping("/getReviewByMsgId")
    public ResponseException getReviewByMsgId(String msgId, String token) throws JsonProcessingException {
        return msgReviewService.getReviewByMsgId(msgId, token);
    }
}
